<?php

class Core_Backend_Component_Promotion_Controller_Friendlink extends Dcmp_Component_Controller {

    public function indexAction($pageindex = 1, $pagesize = 20, $sort = 'id', $dir = 'asc') {
        $this->getHistory()->record();
        $collection = App::getModel('promotion/collection_friendlink')
                        ->sortBy($sort, $dir)
                        ->pagination($pageindex, $pagesize)
                        ->load();
        $records = $collection->loadCount();
        return $this->viewResult(array('list' => $collection, 'records' => $records));
    }

    public function createAction() {
        return $this->viewResult();
    }

    public function editAction($id) {
        $model = App::getModel('promotion/friendlink')->load($id);
        return $this->createAction()->setModel($model);
    }

    public function savePostAction($status) {
        $data = $this->getRequest()->getForm('friendlink');
        $model = App::getModel('promotion/friendlink');
        if (isset($data['id']) && !empty($data['id'])) {
            $model->load($data['id']);
        }
        $model->addData($data)->setStatus($status);
        if (($errors = $model->validate() === TRUE)) {
            $model->save();
            $this->getMessager()->addInform($this->__('share.message.success'));
            return $this->redirectResult($this->getHistoryUrl('promotion/friendlink/index'));
        } else {
            $this->getMessager()->addError($this->__('share.message.fail'))
                    ->addError($errors);
            return $this->createAction()->setModel($model);
        }
    }
    
    public function reviewPostAction($ids, $status, $statusmessage = ''){
    	if($status == 'published'){
    		App::getModel('promotion/friendlink')->publish($ids);
    	}elseif($status = 'forbidden'){
    		App::getModel('promotion/friendlink')->forbid($ids, $statusmessage);
    	}
    	return $this->redirectResult($this->getHistoryUrl('promotion/friendlink/index'));    	
    }    
}
